import IndexService from '@services/index.service';
import { httpPost } from '@/http/http';
import { miniMsgContentProps, miniMsgProps, msgProps, readerConfigProps, textMsgProps } from '@interfaces/msg.interface';
import config from 'config';
const { readerAppId }: readerConfigProps = config.get('reader');

class MsgService {
  public indexService = new IndexService();

  // 发送文本信息
  async sendTextMsg(corpId: string, toUserId: string, text: string) {
    const accessToken = await this.indexService.getCorpAccessToken(corpId);
    const url = `message/send?access_token=${accessToken}`;
    const body: textMsgProps = {
      touser: toUserId,
      msgtype: 'text',
      text: {
        content: text,
      },
      agentid: 1000012,
    };
    const res = await httpPost<msgProps>(url, body);
    return res;
  }

  // 发送小程序消息
  async sendMiniMsg(corpId: string, toUser: string, content: miniMsgContentProps) {
    content.appid = readerAppId;
    const body: miniMsgProps = {
      touser: toUser,
      msgtype: 'miniprogram_notice',
      miniprogram_notice: content,
    };
    console.log(body);
    const accessToken = await this.indexService.getCorpAccessToken(corpId);
    const url = `message/send?access_token=${accessToken}`;
    const res = await httpPost<msgProps>(url, body);
    return res.data;
  }
}

export default MsgService;
